package com.homework.dao;

import com.homework.entity.Cat;
import com.homework.entity.Species;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository("catDao")
public class CatDaoImpl implements CatDao {

    @Autowired
    private JdbcTemplate jtl;

    @Override
    public void insertCat(Cat c) {
        System.out.println("CatDaoImpl.insertCat");
        String sql = "insert into t_cat(cno,cname,cage,species,master,image) values (seq_test.nextval,?,?,?,?,?)";
        jtl.update(sql,c.getCname(),c.getCage(),c.getSpecies(),c.getMaster(),c.getImage());
    }

    @Override
    public void updateCat(Cat c) {
        System.out.println("CatDaoImpl.updateCat");
        String sql = "update t_cat set cname = ?,cage = ?,species = ?,master = ?,image = ? where cno = ?";
        jtl.update(sql,c.getCname(),c.getCage(),c.getSpecies().getSno(),c.getMaster(),c.getImage(),c.getCno());
    }

    @Override
    public void deleteCat(Integer cno) {
        System.out.println("CatDaoImpl.deleteCat");
        String sql = "delete from t_cat where cno = ?";
        jtl.update(sql,cno);
    }

    @Override
    public Cat selectCat(Integer cno) {
        System.out.println("CatDaoImpl.selectCat");
        String sql = "select a.cno,a.cname,a.cage,b.sno,b.sname,a.master,a.image " +
                "from t_cat a join t_species b on a.species = b.sno and cno = ?";

        RowMapper<Cat> rowMapper = new RowMapper<Cat>(){
            @Override
            public Cat mapRow(ResultSet resultSet, int i) throws SQLException {
                Cat cat = new Cat();
                Species species = new Species();
                species.setSno(resultSet.getInt("sno"));
                species.setSname(resultSet.getString("sname"));
                cat.setCno(resultSet.getInt("cno"));
                cat.setCname(resultSet.getString("cname"));
                cat.setCage(resultSet.getInt("cage"));
                cat.setSpecies(species);
                cat.setMaster(resultSet.getString("master"));
                cat.setImage(resultSet.getString("image"));
//                cat.toString();
                System.out.println(cat);
                return cat;
            }
        };

        return jtl.queryForObject(sql, rowMapper, cno);
    }

    @Override
    public List<Cat> selectList() {
        System.out.println("CatDaoImpl.selectList");
        String sql = "select a.cno,a.cname,a.cage,b.sno,b.sname,a.master,a.image " +
                "from t_cat a join t_species b on a.species = b.sno order by a.cno";
//        RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
        RowMapper<Cat> rowMapper = new RowMapper<Cat>(){
            @Override
            public Cat mapRow(ResultSet resultSet, int i) throws SQLException {
                Cat cat = new Cat();
                Species species = new Species();
                species.setSno(resultSet.getInt("sno"));
                species.setSname(resultSet.getString("sname"));
                cat.setCno(resultSet.getInt("cno"));
                cat.setCname(resultSet.getString("cname"));
                cat.setCage(resultSet.getInt("cage"));
                cat.setSpecies(species);
                cat.setMaster(resultSet.getString("master"));
                cat.setImage(resultSet.getString("image"));
//                cat.toString();
                System.out.println(cat);
                return cat;
            }
        };
        return jtl.query(sql, rowMapper);
    }

    @Override
    public void upload(Cat c) {
        System.out.println("CatDaoImpl.updateCat");
        String sql = "update t_cat set image = ? where cno = ?";
        jtl.update(sql,c.getImage(),c.getCno());
    }
}
